from conan import ConanFile
from conan.tools.cmake import CMake, CMakeDeps, CMakeToolchain
from conan.tools.env import VirtualBuildEnv

class ThmarConan(ConanFile):
    name = "thmar"
    version = "0.0.0-dev" 
    # Optional metadata
    license = "BSD-2-Clause"
    author = "guyadong 10km0811@sohu.com"
    url = "https://gitee.com/l0km/thmar"
    description = "基于消息系统实现的thrift RPC客户端"
    topics = ("thift","rpc","mqtt")

    package_type = "static-library"
    
    # Binary configuration
    settings = "os", "compiler", "build_type", "arch"
    options = {"fPIC": [True, False], 
        # 是否编译测试代码
        "build_test": [True, False],
        "with_paho":[True,False],
        # 定义支持的嵌入式系统目标设备
        "target_device":[None,"esp32"]}
    default_options = {"fPIC": True, "build_test": False, "with_paho": True, "target_device": None}
    tool_requires = "cmake/[>=3.15.7]"
    # Sources are located in the same place as this recipe, copy them to the recipe
    exports_sources = "CMakeLists.txt", "src/*","sds/*","mqtt/*","cmake/config.cmake.in","unittest/*","conan2/*"

    def requirements(self):
        self.requires("common_source_cpp/1.0.2")
        self.requires("uthash/2.3.0")
        # esp32内置cJSON库不需要依赖外部提供
        if self.options.target_device != "esp32" :
            # 对依赖cJSON库指定transitive_headers=True,
            # 将头文件路传递给依赖的库,以便cmakeDeps生成依赖关系
            self.requires("cjson/[>=1.7.13]",transitive_headers=True)
        if self.options.build_test :
            self.requires("gtest/[>=1.11.0]")
        if self.options.with_paho :
            self.requires("paho-mqtt-c/[>=1.3.13]",options={"ssl":False,"asynchronous":False})

    def config_options(self):
        if self.settings.os == "Windows" or self.options.target_device == "esp32":
            del self.options.fPIC

    def generate(self):
        tc = CMakeToolchain(self)
        tc.variables["BUILD_TEST"] = self.options.build_test
        tc.variables["WITH_PAHO"] = self.options.with_paho
        if self.options.target_device == "esp32":
            tc.variables["TARGET_DEVICE"] = "esp32"
        tc.generate()

        cd = CMakeDeps(self)
        cd.generate()

        env = VirtualBuildEnv(self)
        env.generate()

    def build(self):
        cmake = CMake(self)
        cmake.configure()
        cmake.build()

    def package(self):
        cmake = CMake(self)
        cmake.install()

    def package_id(self):
        del self.info.options.build_test

    def configure(self):
        del self.settings.compiler.libcxx
        del self.settings.compiler.cppstd

    def package_info(self):
        self.cpp_info.libs = ["thmar"]
        self.cpp_info.set_property("cmake_file_name", "thmar")
        # 指定将安装路径加入CMAKE_PREFIX_PATH,以便执行find_package时能扫描到安装路径
        self.cpp_info.builddirs.append(self.package_folder)
        # 抑制conan生成thmar-config.cmake使用thmar自己生成的cmake配置文件 
        self.cpp_info.set_property("cmake_find_mode", "none")
